package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.*;
import com.zero.mapper.FamilyMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.mapper.PayMapper;
import com.zero.mapper.PayTypeMapper;
import com.zero.util.DateUtils;
import com.zero.util.Result;
import com.zero.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PayService {

    @Autowired
    private AssetsDebtService assetsDebtService;

    @Autowired
    private PayMapper payMapper;

    @Autowired
    private PayTypeMapper payTypeMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Transactional
    public Result add(Pay pay, HttpServletRequest request){

        if (pay == null)
            return Result.fail("获取不到添加的信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");

        //更新个人和家庭资产流出
        familyMember  = familyMemberMapper.selectById(familyMember.getId());
        familyMember.setDebt(familyMember.getDebt().add(pay.getMoney()));
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        family.setDebt(family.getDebt().add(pay.getMoney()));
        familyMemberMapper.update(familyMember);
        familyMapper.update(family);


        //添加支出记录
        pay.setMemberId(familyMember.getId());
        pay.setFamilyId(familyMember.getFamilyId());
        int row = payMapper.add(pay);
        if (row == 1)
            return Result.ok("添加支出信息成功");
        return Result.fail("添加支出信息失败");
    }

    @Transactional
    public Result delete(String ids, HttpServletRequest request){
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");

        //获取家庭和个人信息
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        if (ids == null)
            return Result.fail("获取不到要删除的记录");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            Pay pay = payMapper.selectById(Integer.parseInt(id));

            //修改个人和家庭资产流出
            familyMember.setDebt(familyMember.getDebt().subtract(pay.getMoney()));
            family.setDebt(family.getDebt().subtract(pay.getMoney()));
            row += payMapper.delete(Integer.parseInt(id));
        }

        //更新个人和家庭资产流出
        familyMemberMapper.update(familyMember);
        familyMapper.update(family);
        if (row > 0)
            return Result.ok("成功删除"+ row +"条记录");
        return Result.fail("删除失败");
    }

    @Transactional
    public Result update(Pay pay, HttpServletRequest request){

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");
        if (pay == null)
            return Result.fail("获取不到修改的信息");

        //获取家庭和个人信息
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());

        //获取旧记录
        Pay oldPay = payMapper.selectById(pay.getId());

        //修改家庭和个人资产流出
        familyMember.setDebt(familyMember.getDebt().subtract(oldPay.getMoney()).add(pay.getMoney()));
        family.setDebt(family.getDebt().subtract(oldPay.getMoney()).add(pay.getMoney()));

        familyMemberMapper.update(familyMember);
        familyMapper.update(family);

        int row = payMapper.update(pay);
        if (row == 1)
            return Result.ok("修改支出信息成功");
        return Result.fail("修改支出信息失败");
    }

    public Result select(Condition condition, HttpServletRequest request){
        if (condition == null)
            return Result.fail("获取不到具体的查询信息");

        log.info(condition.toString());

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        condition.setFamilyId(familyMember.getFamilyId());
        if (condition.getPage() != null){
            if (condition.getLimit() != null)
                PageHelper.startPage(condition.getPage(), condition.getLimit());
            else
                PageHelper.startPage(condition.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<Pay> pays = payMapper.selectByConditon(condition);

        for (Pay temp : pays) {
            temp.setFamilyMember(familyMemberMapper.selectById(temp.getMemberId()));
            if (temp.getMemberId() == familyMember.getId())
                temp.setFlag(0);
            else
                temp.setFlag(1);
            temp.setPayType(payTypeMapper.selectById(temp.getTypeId()));

        }

        PageInfo pageInfo = new PageInfo(pays);
        return Result.ok(pays, pageInfo.getTotal());
    }

    public List<Pay> getMemberTodayData(Integer memberId){
        if (memberId == null)
            return null;
        Pay payParam = new Pay();
        payParam.setMemberId(memberId);
        payParam.setCreateDate(DateUtils.getToday());
        List<Pay> result = payMapper.select(payParam);
        return result;
    }

    public List<Pay> getFamilyTodayData(Integer familyId){
        if (familyId == null)
            return null;
        Date today = new Date();
        Pay payParam = new Pay();
        payParam.setFamilyId(familyId);
        payParam.setCreateDate(DateUtils.getToday());
        List<Pay> result = payMapper.select(payParam);
        return result;
    }
}
